/**
 * Handle ticket purchase
 */
package cult.ticketing.processor;

import cult.ticketing.bean.ScheduleItem;
import cult.ticketing.dao.MemberDAO;
import java.util.Calendar;

import cult.ticketing.dao.PurchaseRecordDAO;
import cult.ticketing.dao.ScheduleItemDAO;
import java.text.DateFormat;

/**
 * @author 51158282
 *
 */
public class PurchaseProcessor {
	private int memberId;
	private String role;
	private int scheduleItemId;
	private final int BOOKED_SEAT = 1;
	private final int NOT_ALLOW_REFUND_REQUEST = -2;
	private NotificationProcessor notice; 
	private Calendar purchaseTime;
	private int[] availableSeat;
	private final int NON_MEMBER_ID = -1;
	private final int NON_LOYALTY_POINT = -1;
        private String email;
        private String seatIndex;
        private int totalCost;
        private int loyaltyPoint;
        private int refundStatus;
	
	/**
	 * 
	 */
	public PurchaseProcessor(int scheduleItemId) {
		this.scheduleItemId = scheduleItemId;
	}
	
	public PurchaseProcessor(int memberId, int scheduleItemId, String email, String seatIndex, int totalCost, int loyaltyPoint) {
		this.memberId = memberId;
		this.scheduleItemId = scheduleItemId;
                this.email = email;
                this.seatIndex = seatIndex;
                this.totalCost = totalCost;
                this.loyaltyPoint = loyaltyPoint;
	}

        public boolean updateRecord() {
            boolean ifSuccessful = true;
            ScheduleItemDAO scheduleItemDAO = new ScheduleItemDAO();

            if (!scheduleItemDAO.updateBookedSeat(scheduleItemId, seatIndex)){
                ifSuccessful = false;
            }
            else {
                Calendar purchaseTimeCalendar = Calendar.getInstance(); // Get current time
                String purchaseTime = DateFormat.getDateTimeInstance().format(purchaseTimeCalendar.getTime());

                PurchaseRecordDAO purchaseRecordDAO = new PurchaseRecordDAO();
                if (!purchaseRecordDAO.addPurchaseRecord(memberId, email, scheduleItemId, seatIndex, purchaseTime, refundStatus))
                    ifSuccessful = false;
            }

            return ifSuccessful;
        }

        public boolean updateLoyaltyPoint(int resultLp) {
            MemberDAO memberDAO = new MemberDAO();
            return memberDAO.updateLoyaltyPointById(memberId, resultLp);
        }
	
	public boolean purchaseTicket(int refundStatus) {
            boolean isSuccessful = true;
            this.refundStatus = refundStatus;

            if (!updateRecord())
                isSuccessful = false;
            else {
                if (loyaltyPoint != NON_LOYALTY_POINT)
                    if (!updateLoyaltyPoint(loyaltyPoint + totalCost/10))
                        isSuccessful = false;
            }

            return isSuccessful;
	}

	public boolean purchaseTicketByLoyaltyPoint() {
            boolean isSuccessful = true;
            int resultLp = loyaltyPoint - totalCost;
            refundStatus = NOT_ALLOW_REFUND_REQUEST;

            MemberDAO memberDAO = new MemberDAO();
            if (!updateLoyaltyPoint(resultLp))
                isSuccessful = false;
            else {
                isSuccessful = updateRecord();
            }
            
            return isSuccessful;
        }
	
	/**
	 * Calculate loyalty point by cost
	 * @param totalCost
	 * @return loyalty point value
	 */
	private int calculateLoyaltyPoint(int totalCost) {
		int loyaltyPoint = 0; //loyaltyPoint = cost ..... TODO certain logic
		return loyaltyPoint;
	}

	/**
	 * Calculate total cost of the purchase
	 * Use (new PurchaseProcessor(id)).getTotalCost() to retrive it
	 */
	public int calculatePurchase(String seatIndex) {
	    int ticketCount = seatIndex.split(";").length;

            ScheduleItemDAO scheduleItemDAO = new ScheduleItemDAO();
            ScheduleItem scheduleItem = scheduleItemDAO.getScheduleItemById(scheduleItemId);
            return scheduleItem.getPrice() * ticketCount;
	}

	/**
	 * TODO search how to do timer
	 * Hold the seat for 5 min
	 */
	
	private void timer() {
		
	}

}
